package com.example.demo.rabbitMQ.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * <p>Description: 发布订阅模式的配置,包括两个队列和对应的订阅者,
 * 发布者的交换机类型使用fanout(子网广播),
 * 两根网线binding用来绑定队列到交换机 </p>
 * <p>1个生产者，多个消费者</p>
 * <p>每一个消费者都有自己的一个队列</p>
 *
 * @author Eric Lee
 * @version v1.0.0
 * @since 2021/8/3 16:14
 **/
@ConditionalOnProperty(prefix = "rabbitmq", name = "enable", havingValue = "true")
@Configuration
public class FanoutExchangeConfig {

    /**
     * 创建交换机
     * 在使用 FanoutExchange 后，一条消息会发送至所有与其绑定的队列中，而后，监听了对应队列的消费者就可以获取到同一条消息
     *
     * @return
     */
    @Bean
    public FanoutExchange fanoutExchange() {
        FanoutExchange fanoutExchange = new FanoutExchange("fanoutExchange");
        return fanoutExchange;
    }


    /**
     * 创建队列
     *
     * @return
     */
    @Bean
    public Queue myQueue() {
        Queue queue = new Queue("fanoutQueue");
        return queue;
    }

    @Bean
    public Queue myQueue1() {
        Queue queue = new Queue("fanoutQueue1");
        return queue;
    }

    @Bean
    public Queue myQueue2() {
        Queue queue = new Queue("fanoutQueue2");
        return queue;
    }

    /**
     * 绑定队列堆交换机
     *
     * @return
     */
    @Bean
    public Binding binding() {
        Binding binding = BindingBuilder.bind(myQueue()).to(fanoutExchange());
        return binding;
    }

    @Bean
    public Binding binding1() {
        Binding binding = BindingBuilder.bind(myQueue1()).to(fanoutExchange());
        return binding;
    }

    @Bean
    public Binding binding2() {
        Binding binding = BindingBuilder.bind(myQueue2()).to(fanoutExchange());
        return binding;
    }

}
